package cn.stylefeng.guns.cloud.workflow.modular.controller;

import cn.stylefeng.guns.cloud.libs.scanner.annotation.PostResource;
import cn.stylefeng.guns.cloud.libs.scanner.stereotype.ApiResource;
import cn.stylefeng.guns.cloud.model.page.PageResult;
import cn.stylefeng.guns.cloud.model.web.response.ResponseData;
import cn.stylefeng.guns.cloud.workflow.modular.model.params.ModelParam;
import cn.stylefeng.guns.cloud.workflow.modular.model.result.ModelResult;
import cn.stylefeng.guns.cloud.workflow.modular.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


/**
 * 模型控制器
 *
 * @Author xuyuxiang
 * @Date 2019/11/14 19:59
 **/
@RestController
@ApiResource(name = "模型部署管理接口", path = "/model")
@Api(tags = "模型部署管理接口")
public class ModelController {

    @Autowired
    ModelService modelService;

    /**
     * 创建模型
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:57
     **/
    @PostResource(name = "创建模型", path = "/addItem")
    @ApiOperation(value = "创建模型")
    public ResponseData addItem(@RequestBody ModelParam modelParam) {
        return ResponseData.success(modelService.add(modelParam));
    }

    /**
     * 删除模型
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:58
     **/
    @PostResource(name = "删除模型", path = "/delete")
    @ApiOperation(value = "删除模型")
    public ResponseData delete(@RequestParam("modelId") String modelId) {
        modelService.delete(modelId);
        return ResponseData.success();
    }

    /**
     * 部署
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:58
     **/
    @PostResource(name = "部署模型", path = "/deploy")
    @ApiOperation(value = "部署模型")
    public ResponseData deploy(@RequestParam("modelId") String modelId) {
        modelService.deploy(modelId);
        return ResponseData.success();
    }

    /**
     * 预览xml
     *
     * @Author xuyuxiang
     * @Date 2019/11/19 11:54
     **/
    @PostResource(name = "预览模型xml", path = "/preView")
    @ApiOperation(value = "预览模型xml")
    public ResponseData preView(@RequestParam("modelId") String modelId) throws IOException {
        String xmlCode = modelService.preView(modelId);
        return ResponseData.success(xmlCode);
    }

    /**
     * 获取列表
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:59
     **/
    @PostResource(name = "获取模型列表(分页)", path = "/list")
    @ApiOperation(value = "获取模型列表(分页)", response = ModelResult.class)
    public ResponseData list(@RequestBody ModelParam modelParam) {
        PageResult pageResult = modelService.list(modelParam);
        return ResponseData.success(pageResult);
    }

}


